package com.terrynoya.fight.animations
{
	import com.terrynoya.coreLib.MHashMap;

	public class AnimationManager implements IAnimationManager
	{
		private var _time:int;
		
		private var _currentAni:IAnimation;
		private var _currentElement:IAnimationElement;
		
		private var _isAniFinished:Boolean;
		
		private var _elementSwitchTime:int;
		
		private var _animations:MHashMap;
		
		private var _isForeignAnimation:Boolean;
		
		public function AnimationManager(animations:MHashMap = null)
		{
			this._animations = animations;
		}
		
		public function get elementSwitchTime():int
		{
			return _elementSwitchTime;
		}

		public function setLocalAnimation(animationId:int,elementId:int):Boolean
		{
			if(!this._animations.containsKey(animationId))
			{
				return false;
			}
			var ani:Animation = this._animations.getValue(animationId);
			var element:IAnimationElement = ani.getElement(elementId);
			this.setAnimation(ani,element);
			this._isForeignAnimation = false;
			return true;
		}
		
		public function setForeignAnimation(animations:AnimationManager,animateId:int,elementId:int):Boolean
		{
			if(!animations.hasAnimation(animateId))
			{
				return false;
			}
			var ani:Animation = animations.getAnimation(animateId);
			var element:IAnimationElement = ani.getElement(elementId);
			this._isForeignAnimation = true;
			this.setAnimation(ani,element);
			return true;
		}
		
		public function get time():int
		{
			return _time;
		}

		public function get currentAni():IAnimation
		{
			return _currentAni;
		}

		public function get currentElement():IAnimationElement 
		{
			return this._currentElement;
		}
		
		public function update():void
		{
			if(this._currentAni == null || this._currentElement == null)
			{
				return;
			}
			this._isAniFinished = false;
			this._time ++;
			//element.tick == -1时，停止
			if(this._elementSwitchTime == -1)
			{
				return;
			}
			if(this._elementSwitchTime > 1)
			{
				this._elementSwitchTime--;
			}
			else
			{
				var newEle:IAnimationElement = this._currentAni.getNextElement(this._currentElement.id);
				if(newEle.id <= this._currentElement.id)
				{
					this._isAniFinished = true;
				}
				this._currentElement = newEle;
				this._elementSwitchTime = this._currentElement.ticks;
			}
		}
		
		public function hasAnimation(animationId:int):Boolean
		{
			return this._animations.containsKey(animationId);
		}
		
		public function getAnimation(animationId:int):Animation
		{
			return this._animations.getValue(animationId);
		}
		
		private function setAnimation(animation:IAnimation,element:IAnimationElement):void
		{
			this._isAniFinished = false;
			this._currentAni = animation;
			this._currentElement = element;
			this._time = this._currentAni.getElementStartTime(element.id);
			this._elementSwitchTime = element.ticks;
		}
		
		public function get isAniFinished():Boolean 
		{
			return this._isAniFinished;
		}
		
		public function clone():AnimationManager
		{
			var cloned:AnimationManager = new AnimationManager(this._animations);
			return cloned;
		}
	}
}